﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LibrarySolution.ComMethod;
using LibrarySolution.DAO.基础;
using LibrarySolution.DAO.委托;
using LibrarySolution.Model.基础;

namespace LibrarySolution.Frm
{
    public partial class FrmChangPwd : Form
    {
        #region 属性
        private static FrmChangPwd Instance;
        Delegates delefates = new Delegates();
        #endregion

        #region 构造器
        public FrmChangPwd()
        {
            InitializeComponent();
        }

        public FrmChangPwd(Delegates delegates)
        {
            InitializeComponent();
        }
        #endregion

        #region 事件
        public static FrmChangPwd GetInstance(String yhNo)
        {
            Instance = new FrmChangPwd();
            Instance.BringToFront();
            Instance.Text += "用户: " + yhNo;
            Instance.MdiParent = GlobalVar.frmMain;
            GlobalVar.frmMain.panel1.Controls.Add(Instance);
            Instance.BringToFront();
            return Instance;
        }

        public static FrmChangPwd GetInstance(Delegates delegates)
        {
            Instance = new FrmChangPwd(delegates);
            Instance.BringToFront();
            Instance.delefates = delegates;
            return Instance;
        }

        private void BtnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void TxtOldPwd_Validated(object sender, EventArgs e)
        {

            if (GlobalVar.YhEnter == true)
            {
                if (TxtOldPwd.Text != GlobalVar.YhxxEntity.yh_pwd)
                {
                    if (!BtnCancle.Focused)
                    {
                        TxtOldPwd.Focus();
                        MessageBox.Show("密码错误请再次输入!");
                    }
                }
            }
            else
            {
                if (TxtOldPwd.Text != GlobalVar.GlyEntity.gly_pwd)
                {
                    if (!BtnCancle.Focused)
                    {
                        TxtOldPwd.Focus();
                        MessageBox.Show("密码错误请再次输入!");
                    }
                }
            }
        }

        private void TxtNewPwd2_Validated(object sender, EventArgs e)
        {
            if (TxtNewPwd.Text != TxtNewPwd2.Text)
            {
                if (!BtnCancle.Focused && !TxtNewPwd.Focused)
                {
                    TxtNewPwd2.Focus();
                    MessageBox.Show("二次密码输入不相同");
                }
            }
        }

        private void BtnDone_Click(object sender, EventArgs e)
        {
            if (GlobalVar.YhEnter==true)
            {
                YhDao yhDao = new YhDao();
                YhxxEntity yhxxEntity = new YhxxEntity();
                yhxxEntity.yh_no = GlobalVar.YhxxEntity.yh_no;
                yhxxEntity.yh_pwd = TxtNewPwd2.Text;
                GlobalVar.YhxxEntity.yh_pwd = TxtNewPwd2.Text;
                yhDao.updataYhPwd(yhxxEntity);
                MessageBox.Show("修改成功");
                delefates.doWork();
            }
            else
            {
                GlyDao glyDao = new GlyDao();
                GlyEntity glyEntity = new GlyEntity();
                glyEntity.gly_code = GlobalVar.GlyEntity.gly_code;
                glyEntity.gly_zh = GlobalVar.GlyEntity.gly_zh;
                glyEntity.gly_pwd = TxtNewPwd2.Text;
                GlobalVar.GlyEntity.gly_pwd = TxtNewPwd2.Text;
                glyDao.updataGlyPwd(glyEntity);
                MessageBox.Show("修改成功");
                delefates.doWork();
            }
            this.Close();
        }

        private void TxtNewPwd_Validated(object sender, EventArgs e)
        {
            if (TxtNewPwd.Text.Length < 3)
            {
                if (!BtnCancle.Focused)
                {
                    TxtNewPwd.Focus();
                    MessageBox.Show("输入的密码过短");
                }
            }
        }

        private void FrmChangPwd_FormClosing(object sender, FormClosingEventArgs e)
        {
            GlobalVar.YhEnter = false;
        }
        #endregion

    }
}
